package com.chwn.curiousdiary.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.chwn.curiousdiary.DiaryApplication;
import com.chwn.curiousdiary.R;
import com.chwn.curiousdiary.api.IHomeApi;
import com.chwn.curiousdiary.api.NetWork;
import com.chwn.curiousdiary.bean.StoryBean;
import com.chwn.curiousdiary.bean.StoryListBean;
import com.chwn.curiousdiary.gloable.CustomEvent;
import com.chwn.curiousdiary.utils.DialogUtil;
import com.chwn.curiousdiary.utils.PopupWindowUtils;
import com.chwn.harulib.base.BaseActivity;
import com.chwn.harulib.mvp.rx.HttpResult;
import com.chwn.harulib.util.ScreenUtil;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMusic;

import de.greenrobot.event.EventBus;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by Myshikin on 2017/8/9.
 */

public abstract class BaseShareActivity extends BaseActivity {

    private StoryBean     storyBean;
    private StoryListBean storyListBean;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    public void onEventMainThread(CustomEvent event) {
        switch (event.getState()) {
            case CustomEvent.SHARE_STORY:
                storyBean = null;
                storyListBean = null;
                if (event.getMessage() instanceof StoryBean) {
                    storyBean = (StoryBean) event.getMessage();
                } else {
                    storyListBean = (StoryListBean) event.getMessage();
                }
                shareTwo();
                break;
        }
    }

    private PopupWindow mPopWindow;

    protected void shareTwo() {

        View contentView = LayoutInflater.from(this).inflate(R.layout.layout_share, null);
        mPopWindow = new PopupWindow(contentView,
                RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.MATCH_PARENT, true);
        mPopWindow.setContentView(contentView);

        contentView.findViewById(R.id.iv_qq).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickShare(v);
            }
        });
        contentView.findViewById(R.id.iv_qq_friend).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickShare(v);
            }
        });
        contentView.findViewById(R.id.iv_wechat).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickShare(v);
            }
        });
        contentView.findViewById(R.id.iv_wechat_friend).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickShare(v);
            }
        });
        contentView.findViewById(R.id.iv_share_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickShare(v);
            }
        });

        mPopWindow.setOutsideTouchable(true);

        mPopWindow.setAnimationStyle(PopupWindowUtils.ANIMATION_BOTTOM);
        mPopWindow.showAtLocation(getLocationView(), Gravity.BOTTOM, 0, ScreenUtil.getBottomStatusHeight(BaseShareActivity.this));

    }

    /**
     * PopupWindow根据此View进行显示
     * @return
     */
    protected abstract View getLocationView();

    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA share_media) {
            DialogUtil.showWaitDialog(BaseShareActivity.this, "", true);
        }

        @Override
        public void onResult(SHARE_MEDIA platform) {
            DialogUtil.dismissWaitDialog();
            Toast.makeText(BaseShareActivity.this, " 分享成功", Toast.LENGTH_SHORT).show();
            uploadShareInfo();
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            DialogUtil.dismissWaitDialog();
            Toast.makeText(BaseShareActivity.this, " 分享失败", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            DialogUtil.dismissWaitDialog();
        }
    };

    private void uploadShareInfo() {
        String userToken = DiaryApplication.mInstance.getUserToken();
        String storyId = getStoryId();
        if (!TextUtils.isEmpty(userToken) && !TextUtils.isEmpty(storyId)) {
            NetWork.getInstance()
                    .getApiService(IHomeApi.class)
                    .shareStory(storyId, userToken, 3)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Action1<HttpResult<Object>>() {
                        @Override
                        public void call(HttpResult<Object> objectHttpResult) {

                        }
                    }, new Action1<Throwable>() {
                        @Override
                        public void call(Throwable throwable) {
                        }
                    });
        }
    }

    String content = "好奇日记";

    private void onClickShare(View v) {
        UMImage umImage = new UMImage(BaseShareActivity.this, R.mipmap.ic_launcher);
        UMImage storyImage = new UMImage(BaseShareActivity.this, getStoryImage());

        UMusic music = new UMusic(getStoryUrl());
        music.setTitle(getStoryTitle());//音乐的标题
        music.setThumb(storyImage);//音乐的缩略图
        music.setDescription(getStoryDetailTitle());
        switch (v.getId()) {
            case R.id.iv_qq:
                music.setmTargetUrl(getStroyShareURL());
                new ShareAction(BaseShareActivity.this)
                        .setPlatform(SHARE_MEDIA.QQ)
                        .withText(content)
                        .withMedia(umImage)
                        .withMedia(music)
                        .setCallback(umShareListener)
                        .share();
                break;
            case R.id.iv_qq_friend:
                music.setmTargetUrl(getStroyShareURL());
                new ShareAction(BaseShareActivity.this)
                        .setPlatform(SHARE_MEDIA.QZONE)
                        .setCallback(umShareListener)
                        .withText(content)
                        .withMedia(umImage)
                        .withMedia(music)
                        .share();
                break;
            case R.id.iv_wechat:
                music.setmTargetUrl(getStroyShareURL());
                new ShareAction(BaseShareActivity.this)
                        .setPlatform(SHARE_MEDIA.WEIXIN)
                        .setCallback(umShareListener)
                        .withText(content)
                        .withMedia(umImage)
                        .withMedia(music)
                        .share();
                break;
            case R.id.iv_wechat_friend:
                music.setmTargetUrl(getStroyShareURL());
                new ShareAction(BaseShareActivity.this)
                        .setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE)
                        .setCallback(umShareListener)
                        .withText(content)
                        .withMedia(umImage)
                        .withMedia(music)
                        .share();

                break;
            case R.id.iv_share_close:
                break;
        }

        mPopWindow.dismiss();
    }

    private String getStoryImage() {
        if (storyBean != null) {
            return storyBean.getStoryImage();
        }
        if (storyListBean != null) {
            return storyListBean.getStoryImage();
        }
        return "";
    }

    private String getStoryUrl() {
        if (storyBean != null) {
            return storyBean.getStoryUrl();
        }
        if (storyListBean != null) {
            return storyListBean.getStoryUrl();
        }
        return "";
    }

    private String getStroyShareURL() {
        if (storyBean != null) {
            return storyBean.getStroyShareURL();
        }
        if (storyListBean != null) {
            return storyListBean.getStroyShareURL();
        }
        return "";
    }

    private String getStoryTitle() {
        if (storyBean != null) {
            return storyBean.getStoryTitle();
        }
        if (storyListBean != null) {
            return storyListBean.getStoryTitle();
        }
        return "";
    }

    private String getStoryDetailTitle() {
        if (storyBean != null) {
            return storyBean.getStoryDetailTitle();
        }
        if (storyListBean != null) {
            return storyListBean.getStoryDetailTitle();
        }
        return "";
    }

    private String getStoryId() {
        if (storyBean != null) {
            return storyBean.getStoryId();
        }
        if (storyListBean != null) {
            return storyListBean.getStoryId();
        }
        return "";
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
